package com.xinqi.modules.prepare.folder.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.mybatis.data.repo.SuperMapper;
import com.xinqi.modules.prepare.folder.domain.PrepareFileFolderEntity;
import com.xinqi.modules.prepare.folder.dto.req.PrepareFileFolderQueryDTO;
import com.xinqi.modules.prepare.folder.dto.rsp.PrepareFileFolderResultDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 备课区文件夹数据层
 *
 * @author: system
 * @date: 2023/03/19
 */
@Mapper
public interface PrepareFileFolderMapper extends SuperMapper<PrepareFileFolderEntity> {
    Page<PrepareFileFolderResultDTO> findPage(Page<Object> page, @Param("dto") PrepareFileFolderQueryDTO query);

    List<PrepareFileFolderResultDTO> findList(@Param("dto") PrepareFileFolderQueryDTO query);

    PrepareFileFolderEntity findDeletedById(@Param("id") Long id);

    PrepareFileFolderEntity findDeletedByRefId(@Param("refId") Long refId);

    int restoreById(@Param("id") Long id);

    int maxLevel(@Param("prepareId") Long prepareId, @Param("type") String type, @Param("filePath") String filePath);

    long fileSizeTotal(@Param("prepareId") Long prepareId);

    @Select("select IFNULL(MAX(CAST(SUBSTRING_INDEX(a.name, '_', -1) AS SIGNED) + 1), 1) AS result from pl_prepare_file_folder a where a.prepare_id = #{prepareId} and a.type = #{type} and a.pid = #{pid} and a.file_type = #{fileType} and a.name like concat(#{name}, '#')")
    Long getMaxNum(
        @Param("prepareId") Long prepareId, @Param("type") String type,
        @Param("name") String name, @Param("pid") Long pid, @Param("fileType") String fileType
    );
}
